package com.lv.dhart.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

import com.lv.dhart.R;
import com.lv.dhart.beans.as4kdots.CostChangeBeans;
import com.lv.dhart.beans.cost.AddResultBean;
import com.lv.dhart.beans.cost.DeleteBean;
import com.lv.dhart.callback.cost.ICostAddCallback;
import com.lv.dhart.callback.cost.ICostListCallback;
import com.lv.dhart.callback.cost.IDeleteCostCallback;
import com.lv.dhart.constant.ApiPathConstant;
import com.lv.dhart.custom.fragment.baseframework.BaseActivity;
import com.lv.dhart.custom.fragment.baseframework.BaseFragment;
import com.lv.dhart.custom.fragment.baseframework.interfaces.Layout;
import com.lv.dhart.custom.fragment.baseframework.util.JumpParameter;
import com.lv.dhart.holder.cost.ListCostViewHolderHelper;
import com.lv.dhart.http.rxeasy.utils.HttpLog;
import com.lv.dhart.presenter.CostPresenter;
import com.lv.dhart.token.TokenManager;
import com.lv.dhart.ui.activity.CostActivity;
import com.lv.dhart.utilcode.util.ToastUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.smart.holder.CommonAdapter;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成本列表
 */
@Layout(R.layout.fragment_mook)
public class CostFragment extends BaseFragment<BaseActivity> implements AdapterView.OnItemClickListener, OnRefreshListener, OnLoadMoreListener, ICostListCallback, ICostAddCallback, ListCostViewHolderHelper.ListCostViewHolderClickListener, IDeleteCostCallback {
    protected ListView mListView;
    protected AppCompatButton btnAddStockCodeCost;
    protected EditText etAddStockCodeCost;
    private final List<CostChangeBeans.DataDTO> mDataBeanList = new ArrayList<>();
    private CommonAdapter<CostChangeBeans.DataDTO> commonAdapter;
    private int offset = 0;
    private int limit = 10;
    private CostPresenter costPresenter;
    private RefreshLayout refreshLayout;

    @Override
    public void initViews() {
        mListView = findViewById(R.id.id_listview);
        btnAddStockCodeCost = findViewById(R.id.btn_add_stock_code_cost);
        etAddStockCodeCost = findViewById(R.id.et_add_stock_code_cost);
        ListCostViewHolderHelper listCostViewHolderHelper = new ListCostViewHolderHelper();
        listCostViewHolderHelper.setListener(this);
        commonAdapter = new CommonAdapter<>(requireContext(), mDataBeanList, R.layout.item_list_cost,listCostViewHolderHelper);
        mListView.setAdapter(commonAdapter);
        mListView.setOnItemClickListener(this);

        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(requireContext()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(requireContext()));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setEnableLoadMore(false);
    }



    @Override
    public void initDatas() {
        costPresenter = new CostPresenter();
//        new PresenterData(this).getJsonLocal(MUTIL_OBJECT);//取本地字符串
        getStockList(null);
    }

    private void getStockList(RefreshLayout refreshLayout) {
        String token = TokenManager.getInstance().getAuthModel().getAccessToken();
        HttpLog.i("token", token);
        costPresenter.getStockCostList(offset, limit, this);

    }

    @Override
    public void onLoad() {
        super.onLoad();
        getStockList(null);
    }

    @Override
    public void setEvents() {
        btnAddStockCodeCost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stock_code = etAddStockCodeCost.getText().toString();
                if (TextUtils.isEmpty(stock_code)) {
                    ToastUtils.showLong("股票代码不能为空！");
                }
                HashMap<String, String> params = new HashMap<>();
                params.put("stock_code", stock_code);
                JSONObject jsonObject = new JSONObject(params);
                costPresenter.addCostStockCode(params, CostFragment.this);
            }
        });
    }


    private String addPathParam(int offset, int limit) {
        return ApiPathConstant.API_AS_COST_LIST + "/" + offset + "/" + limit;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> dataMa = new HashMap<>();
        dataMa.put("cost", mDataBeanList.get(position));
        HttpLog.i("mDataBeanList.get(position)", mDataBeanList.get(position).getAddAmount());
        HttpLog.i("mDataBeanList.get(position)", String.valueOf(position));
        jump(CostActivity.class, new JumpParameter(dataMa));
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        HttpLog.i("refreshLayout");
        getStockList(refreshLayout);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onCostListSuccess(CostChangeBeans costChangeBeans) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
        mDataBeanList.clear();
        mDataBeanList.addAll(costChangeBeans.getData());
        commonAdapter.notifyDataSetChanged();
    }


    @Override
    public void onCostListFailure(Throwable throwable) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh(false);
        }
        HttpLog.e(throwable.getMessage());
    }

    @Override
    public void onCostAddSuccess(AddResultBean addResultBean) {
        if (addResultBean.getData() < 0) {
            ToastUtils.showLong("添加失败");
        } else {
            ToastUtils.showLong("添加成功");
            getStockList(null);
        }
    }

    @Override
    public void onCostAddFailure(Throwable throwable) {

    }

    @Override
    public void onDeleteClick(View view, int position) {
        costPresenter.deleteItemById(mDataBeanList.get(position).getId(),this);
    }

    @Override
    public void onDeleteCostFailure(Throwable throwable) {

    }

    @Override
    public void onDeleteCostSuccess(DeleteBean deleteBean) {
        getStockList(null);
    }
}